package com.lmqb.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.aliyun.oss.OSSClient;
import com.lmqb.bean.SysActivity;
import com.lmqb.bean.SysConfig;
import com.lmqb.bean.SysNotice;
import com.lmqb.bean.SysUser;
import com.lmqb.constant.FileIO;
import com.lmqb.entry.MenuTitle;
import com.lmqb.page.ActivePager;
import com.lmqb.page.EmailLogPager;
import com.lmqb.page.FeedbackPager;
import com.lmqb.page.InnerMsgPager;
import com.lmqb.page.NoticePager;
import com.lmqb.page.QuestionPager;
import com.lmqb.page.SmsCodePager;
import com.lmqb.service.CfgParamsService;
import com.lmqb.service.JiaoYanService;
import com.lmqb.service.MsgService;
import com.lmqb.service.SysUserService;

@Controller
public class MsgController extends BaseController{

	@Autowired
	private JiaoYanService jiaoYanService;
	
	@Autowired
	private MsgService msgService;
	
	@Autowired
    private SysUserService sysUserService;
	
	@Autowired
	private CfgParamsService cfgParamsService;
	
	//跳转到消息公告页面
	@RequestMapping(value="notice.html")
	public String toNotice(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,NoticePager pagerModel){
		
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(NoticePager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			NoticePager noticePager=msgService.selectNoticePager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, noticePager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("消息管理");
			menuTitle.setMenu("消息公告");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "noticeList";
	}
	
	//跳转到添加消息公告页面
	@RequestMapping(value="addNotice")
	public String addNotic(){
		
		return "addNotice";
	}
	//添加公告
	@RequestMapping(value="addSmg")
	public String addSmg(Integer adminId,String noticeTitle,String noticeContent,Byte openStatus) throws UnsupportedEncodingException{
		noticeTitle = new String(noticeTitle.getBytes("iso8859-1"),"utf-8");
		noticeContent = new String(noticeContent.getBytes("iso8859-1"),"utf-8");
		SysNotice sysNotice=new SysNotice();
		sysNotice.setAdminId(adminId);
		sysNotice.setNoticeTitle(noticeTitle);
		sysNotice.setNoticeContent(noticeContent);
		sysNotice.setOpenStatus(openStatus);
		msgService.addSmg(sysNotice);
		return "redirect:notice.html";
	}
	
	//跳转到编辑消息公告页面
	@RequestMapping(value="modifyNotice")
	public String modifyNotice(Integer adminId,Integer noticeId,Map<String, Object>map){
		SysNotice  sysNotice= msgService.selectByPrimaryKey(noticeId);
		SysUser selectById = sysUserService.selectById(adminId);
		System.out.println(sysNotice);
		map.put("notice", sysNotice);
		map.put("auther", selectById.getRealName());
		return "modifyNotice";
	}
	//修改消息公告
	@RequestMapping(value="modifySmg")
	public String modifySmg(Integer noticeId,Integer adminId,String noticeTitle,String noticeContent,Byte openStatus) throws UnsupportedEncodingException{
		noticeTitle = new String(noticeTitle.getBytes("iso8859-1"),"utf-8");
		noticeContent = new String(noticeContent.getBytes("iso8859-1"),"utf-8");
		SysNotice sysNotice=new SysNotice();
		sysNotice.setNoticeId(noticeId);
		sysNotice.setAdminId(adminId);
		sysNotice.setNoticeTitle(noticeTitle);
		sysNotice.setNoticeContent(noticeContent);
		sysNotice.setOpenStatus(openStatus);
		msgService.updateByPrimaryKeySelective(sysNotice);
		return "redirect:notice.html";
	}
	
	//删除消息公告
	@RequestMapping("deleteNotice")
	public String deleteNotice(Integer noticeId){
		System.out.println("进入删除消息");
		msgService.deleteByPrimaryKey(noticeId);
		return "redirect:notice.html";
		
	}
		
	//热门活动列表
	@RequestMapping(value="active.html")
	public String toActive(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,ActivePager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(ActivePager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			ActivePager noticePager=msgService.selectActivePager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, noticePager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("消息管理");
			menuTitle.setMenu("热门活动");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		SysConfig ossPrefix = cfgParamsService.selectOssPrefix();
		request.setAttribute("URL",ossPrefix.getPmValue());
		return "activeList";
	}
	
	//跳转到添加活动页面
	@RequestMapping(value="addActive")
	public String addActive(){
			
			return "addActive";
		}
	//添加活动
	@RequestMapping(value="addHotActive",method=RequestMethod.POST)
	public String addHotActive(SysActivity sysActivity,MultipartFile file1) throws UnsupportedEncodingException{
		OSSClient ossClient = new OSSClient(FileIO.endpoint, FileIO.accessKeyId, FileIO.accessKeySecret);
		try {
			System.out.println("上传成功");
		    Calendar now = Calendar.getInstance();
		    String key="manage/"+now.get(Calendar.YEAR)+"/"+(now.get(Calendar.MONTH) + 1)+"/"+now.get(Calendar.DAY_OF_MONTH)+"/"+now.getTimeInMillis()+".data";
		    ossClient.putObject(FileIO.bucket, key, file1.getInputStream());
		    sysActivity.setThumbnailUrl(key);
		} catch (Exception e1) {
			System.out.println("上传失败");
			e1.printStackTrace();
		}
		 int i = msgService.addActive(sysActivity);
			return "redirect:active.html";
		}
	
	//删除热门活动
	@RequestMapping(value="deleteActive")
	public String deleteActive(Integer activityId){
		msgService.deleteByPrimaryKeyA(activityId);
		return "redirect:active.html";
		}
	
	//跳转到修改活动页面
	@RequestMapping(value="modifyActive")
	public String modifyActive(Integer activityId,Map<String, Object>map){
		System.out.println("修改热门活动回显"+activityId);
		SysActivity activity = msgService.selectActiveByPrimaryKey(activityId);
		System.out.println("热门活动回显-->"+activity);
		SysConfig ossPrefix = cfgParamsService.selectOssPrefix();
		map.put("URL", ossPrefix.getPmValue());
		map.put("active", activity);
				return "updateActive";
			}
	//修改热门活动
	@RequestMapping(value="updateHotActive",method=RequestMethod.POST)
	public String updateHotActive(SysActivity sysActivity){
        int updateActiveByPrimaryKey = msgService.updateActiveByPrimaryKey(sysActivity);
		return "redirect:active.html";
	}
	
	//修改热门活动图片
	@RequestMapping(value="updateActiveP",method=RequestMethod.POST)
	public String updateActiveP(SysActivity sysActivity,MultipartFile file){
		OSSClient ossClient = new OSSClient(FileIO.endpoint, FileIO.accessKeyId, FileIO.accessKeySecret);
		try {
		    Calendar now = Calendar.getInstance();
		    String key="manage/"+now.get(Calendar.YEAR)+"/"+(now.get(Calendar.MONTH) + 1)+"/"+now.get(Calendar.DAY_OF_MONTH)+"/"+now.getTimeInMillis()+".data";
		    ossClient.putObject(FileIO.bucket, key, file.getInputStream());
		    sysActivity.setThumbnailUrl(key);
		} catch (Exception e1) {
			System.out.println("上传失败");
			e1.printStackTrace();
		}
		System.out.println(sysActivity.getThumbnailUrl());
		msgService.updateByPrimaryKeySelectiveP(sysActivity);
		return "redirect:active.html";
	}
	
	//常见问题列表
	@RequestMapping(value="question.html")
	public String toQuestion(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,QuestionPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(QuestionPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			QuestionPager noticePager=msgService.selectQuestionPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, noticePager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("消息管理");
			menuTitle.setMenu("常见问题");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "questionList";
	}
	
	//站内信
	@RequestMapping(value="innerMsg.html")
	public String toInneerMsg(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,InnerMsgPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(InnerMsgPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			InnerMsgPager innerMsgPager=msgService.selectInnerMsgPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, innerMsgPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("消息管理");
			menuTitle.setMenu("站内信");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "innerMsgList";
	}
	
	//意见反馈
	@RequestMapping(value="feedback.html")
	public String toFeedback(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,FeedbackPager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(FeedbackPager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			FeedbackPager feedBackPager=msgService.selectFeedBackPager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, feedBackPager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("消息管理");
			menuTitle.setMenu("意见反馈");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "feedbackList";
	}
	
	//删除一件反馈
	@RequestMapping("deleteFeedback")
	public String deleteFeedback(Integer feedbackId){
		msgService.deleteByPrimaryKeyF(feedbackId);
		return "redirect:feedback.html";
	}
	
	//修改反馈意见状态
	@RequestMapping("updateFeedback")
	public String updateFeedback(Integer feedbackId){
		System.out.println("意见反馈状态----"+feedbackId);
		msgService.updateStatusById(feedbackId);
		return "redirect:feedback.html";
	}
	//短信验证码
	@RequestMapping(value="smsCode.html")
	public String toSmsCode(HttpSession session,HttpServletRequest request,
			HttpServletResponse response,SmsCodePager pagerModel){
		try{
			SysUser sysUser=(SysUser)session.getAttribute(memberAdmin);
			if(sysUser==null){
				return "login";
			}
			pagerModel=(SmsCodePager) jiaoYanService.pagerModelFormat(pagerModel);
			setIfEmpty(pagerModel);
			SmsCodePager smsCodePager=msgService.selectSmsCodePager(pagerModel);
			setIfEndty(pagerModel);
			request.setAttribute(pager, smsCodePager);
			MenuTitle menuTitle=new MenuTitle();
			menuTitle.setParent("日志管理");
			menuTitle.setMenu("验证码列表");
			session.setAttribute(menuTitleF, menuTitle);
		}catch(Exception e){
			e.printStackTrace();
		}
		return "smsCodeList";
	}
}
